var $ = require('../../utils/Ajax.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
Page({
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    Id: null,
    ExperDetails: null,
    // 评价
    ListComment: null,
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    // 是否收藏 0-未收藏 1-收藏
    Collection: null,
    CollectionImg: '../../images/sc_nor.png',
    DAte: null,
    Time: null,
    // 注意事项循环
    Careful: null,
    // 活动内容循环
    Content: null,
    // 转发标题
    Title:null
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "体验详情"
    })
    this.setData({
      Id: options.Id
    })
    $.Ajax("api/x_list_exp_info", "GET", {
      id: options.Id
    }, function (res) {
      that.setData({
        imgUrls: res.d.experience.image,
        ExperDetails: res.d.experience,
        ListComment: res.d.list_comment,
        Collection: res.d.is_collection,
        DAte: res.d.experience.date,
        Time: res.d.experience.time,
        Careful: res.d.experience.careful,
        Content: res.d.experience.content,
        Title: res.d.experience.title
      })
      if (res.d.is_collection == 1){
        that.setData({
          CollectionImg: '../../images/sc_sel.png'
        })
      }
      if (res.d.experience.image.length == 1){
        that.setData({
          indicatorDots: false
        })
      }
    });
  },

  onShow: function(){
    // 进入页面自动轮播,防止轮播图片放大关闭后不轮播
    this.setData({
      autoplay: true
    })
  },

  ExperPlaceOrder: function (e) {
    var Id = e.currentTarget.dataset.id;
    var Price = e.currentTarget.dataset.price;
    var Careful = e.currentTarget.dataset.careful;
    wx.navigateTo({
      url: '../ExperPlaceOrder/ExperPlaceOrder?Id=' + Id + "&Price=" + Price + '&DAte=' + this.data.DAte + '&Time=' + this.data.Time + '&Careful=' + Careful
    })
  },

  // 点击轮播图,图片放大
  SwiperImg: function(e){
    var that = this;
    var Urls = [];
    var PictureUrl = this.data.PictureUrl;
    var swiperurl = e.currentTarget.dataset.swiperurl;
    var swiperimg = e.currentTarget.dataset.swiperimg;
    for (var i = 0; i < swiperurl.length; i++) {
      Urls.push(PictureUrl + swiperurl[i]);
    }
    wx.previewImage({
      current: swiperimg,
      urls: Urls,
      success: function(){
        that.setData({
          autoplay: false
        })
      }
    })
  },

  // 点击分享
  Share: function (e) {
    var that = this;
    var Id = e.currentTarget.dataset.id;
    var itemList = ["保存为图片"];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function (ActionSheet) {
        wx.navigateTo({
          url: '../Collection/Collection?Type=1&Id=' + Id,
        })
      }
    })
  },

  // 转发
  onShareAppMessage: function () {
    var Title = this.data.Title;
    return {
      title: Title,
      path: '/pages/ExperDetails/ExperDetails?Id=' + this.data.Id,
      success: function (res) {
        wx.showToast({
          title: "分享成功",
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        wx.showToast({
          title: "分享失败",
          icon: 'none',
          image: '../../images/err.png',
          duration: 2000
        })
      }
    }
  },

  // 点击收藏
  Collection: function(){
    var that = this;
    var Token = wx.getStorageSync('Token');
    if (Token == '' || Token == null) {
      wx.showToast({
        title: '请重新登录',
        icon: 'success',
        image: '../../images/err.png',
        duration: 1000,
        mask: true
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../My/My',
        })
      }, 1000)
    } else {
      $.Ajax("api/x_add_coll", "POST", {
        t: 1,
        id: that.data.Id
      }, function (res) {
        if (res.s) {
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 3000
          });
          if (res.m == '收藏成功') {
            that.setData({
              CollectionImg: '../../images/sc_sel.png'
            })
          } else {
            that.setData({
              CollectionImg: '../../images/sc_nor.png'
            })
          }
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 3000
          });
        }
      });
    }
  },

  // 点击图片放大
  ClickImg: function(e){
    var Urls = [];
    var PictureUrl = this.data.PictureUrl;
    var imgurl = e.currentTarget.dataset.imgurl;
    var img = e.currentTarget.dataset.img;
    for (var i = 0; i < imgurl.length;i++){
      Urls.push(PictureUrl + imgurl[i]);
    }
    wx.previewImage({
      current: img,
      urls: Urls
    })
  },

  // 点击位置导航
  SetOut: function (e) {
    var Address = e.currentTarget.dataset.address;
    wx.showModal({
      title: '位置导航',
      content: '你确定要打开前往 ' + Address +' 的路线导航吗？',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          var wxMap = new QQMapWX({
            key: 'YQ6BZ-XJXKI-VSIGO-5ILBX-SLBK5-OVFHK'
          });
          // 商户所在位置经纬度
          wxMap.geocoder({
            address: Address,
            success: function (res) {
              wx.openLocation({
                latitude: res.result.location.lat,
                longitude: res.result.location.lng,
                name: Address,
                scale: 28
              })
            }
          });
        } else {
          return false;
        }
      }
    });
  }
})